﻿using BasicModuleOrm.Db_Context;
using BasicModuleOrm.Model; 
namespace BasicModuleOrm.ProductRespository
{
    public class ProductSqlSugarRespository: IProductRespository
    {
        private SqlSugarContext _sqlSugarContext;

        public ProductSqlSugarRespository(SqlSugarContext sqlSugarContext)
        {
            _sqlSugarContext = sqlSugarContext;
        }

        public Product AddProduct(Product product)
        {
            // 1、添加商品
            _sqlSugarContext.sqlSugarClient.Insertable(product).ExecuteCommand();

            return product;
        }

        public void DeleteProduct(int id)
        {
            // 1、查询商品
            var p = _sqlSugarContext.sqlSugarClient.Queryable<Product>().InSingle(id);

            // 2、更新商品
            _sqlSugarContext.sqlSugarClient.Deleteable<Product>(id).ExecuteCommand();
        }

        public IEnumerable<Product> GetAllProduct()
        {
            //1、查询所有的商品
            var product = _sqlSugarContext.sqlSugarClient.Queryable<Product>().ToList();
            return product;
        }

        public Product GetProduct(int id)
        {
            //1、查询所有的商品
            var Product = _sqlSugarContext.sqlSugarClient.Queryable<Product>().InSingle(id);

            //2、返回商品
            return Product;
        }

        public void UpdateProduct(int id, Product product)
        {
            // 1、查询商品
            var p = _sqlSugarContext.sqlSugarClient.Queryable<Product>().InSingle(id);

            // 2、更新商品
            _sqlSugarContext.sqlSugarClient.Updateable(product).ExecuteCommand();

        }
    }
}
